package main.java.com.fxy.dao.impl;


import main.java.com.fxy.dao.UserDao;
import main.java.com.fxy.pojo.User;
import main.java.com.fxy.util.DBUtils;

import java.sql.*;
import java.time.LocalDateTime;

public class UserDaoImpl implements UserDao {
    @Override
    public boolean login(User user) {
        Connection conn = DBUtils.getConn();

        String sql="select * from user where username = '" + user.getUsername() +"' and password ='" + user.getPassword() +"'";
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean regist(User user) {
        Connection conn = DBUtils.getConn();

        String sql="select * from user where username = '" + user.getUsername() +"'";
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            if(rs.next()){

                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sql ="insert into user (username,password,create_time,email) values('" + user.getUsername() +"','"+ user.getPassword()+ "','" + LocalDateTime.now()+"','"+user.getEmail() + "')";
        try {
            System.out.println(sql);
            PreparedStatement statement = conn.prepareStatement(sql);
            boolean execute = statement.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}

